package com.alander.nexusgo.im.service;

import com.alander.nexusgo.im.model.entity.GroupMessage;
import com.alander.nexusgo.im.model.entity.SingleMessage;
import com.alander.nexusgo.im.mapper.GroupMessageMapper;
import com.alander.nexusgo.im.mapper.SingleMessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class MessageStorageService {
    
    @Autowired
    private SingleMessageMapper singleMessageMapper;
    
    @Autowired
    private GroupMessageMapper groupMessageMapper;
    
    /**
     * 存储单聊消息
     * @param senderId 发送者ID
     * @param receiverId 接收者ID
     * @param content 消息内容
     * @return 是否存储成功
     */
    public boolean saveSingleMessage(Long senderId, Long receiverId, String content) {
        try {
            SingleMessage message = new SingleMessage();
            message.setSenderId(senderId);
            message.setReceiverId(receiverId);
            message.setMessageId(UUID.randomUUID().toString());
            message.setContent(content);
            message.setStatus(0); // 未读
            message.setCreateTime(LocalDateTime.now());
            
            return singleMessageMapper.insert(message) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * 存储群聊消息
     * @param groupId 群组ID
     * @param senderId 发送者ID
     * @param content 消息内容
     * @return 是否存储成功
     */
    public boolean saveGroupMessage(Integer groupId, Long senderId, String content) {
        try {
            GroupMessage message = new GroupMessage();
            message.setGroupId(groupId);
            message.setSenderId(senderId);
            message.setMessageId(UUID.randomUUID().toString());
            message.setContent(content);
            message.setCreateTime(LocalDateTime.now());
            
            return groupMessageMapper.insert(message) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * 获取两个用户之间的聊天记录
     * @param userId1 用户1 ID
     * @param userId2 用户2 ID
     * @return 消息列表
     */
    public List<SingleMessage> getMessagesBetweenUsers(Long userId1, Long userId2) {
        return singleMessageMapper.selectMessagesBetweenUsers(userId1, userId2);
    }
    
    /**
     * 获取群组最近的消息
     * @param groupId 群组ID
     * @param limit 限制数量
     * @return 消息列表
     */
    public List<GroupMessage> getRecentGroupMessages(Integer groupId, int limit) {
        return groupMessageMapper.selectRecentMessages(groupId, limit);
    }
    
    /**
     * 获取用户未读消息
     * @param receiverId 接收者ID
     * @return 未读消息列表
     */
    public List<SingleMessage> getUnreadMessages(Long receiverId) {
        return singleMessageMapper.selectUnreadMessages(receiverId);
    }
    
    /**
     * 标记消息为已读
     * @param receiverId 接收者ID
     * @param senderId 发送者ID
     * @return 更新记录数
     */
    public int markMessagesAsRead(Long receiverId, Long senderId) {
        return singleMessageMapper.updateMessagesAsRead(receiverId, senderId);
    }
}